//
//  MainSelectTypeView.m
//  ToEnjoyDemo
//
//  Created by 薛小刚 on 2017/11/29.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "MainSelectTypeView.h"

#define Color(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define MIN_WIDTH_TYPE 70
#define MORE_BTN_WIDTH 0
@interface MainSelectTypeView()<UIScrollViewDelegate>
/// 该选择器view的frame
@property (nonatomic, assign) CGRect myFrame;
/// 数据源
@property (nonatomic, strong) NSArray * dataArray;
/// 每一个按钮的宽度
@property (nonatomic, assign) CGFloat width;
/// 底部的标示线
@property (nonatomic, strong) UIView * lineView;
/// 滚动视图
@property (nonatomic, strong) UIScrollView * scrollView;
/// 更多按钮
@property (nonatomic, strong) UIButton * moreButton;
/// 展开后盛放按钮的视图
@property (nonatomic, strong) UIView * backgroundView;
/// 开关,判断是点击,还是滚动 yes 点击 no 滚动
@property (nonatomic, assign) BOOL isOpen;

@end

@implementation MainSelectTypeView

/**
 初始化方法

 @param frame 尺寸
 @param array 数据源
 @return 返回视图
 */
- (instancetype) initWithFrame:(CGRect)frame  withDataArray:(NSArray *) array {
    if (self = [super initWithFrame:frame]) {
        _dataArray = array;
        _myFrame = frame;
        _width = _myFrame.size.width / _dataArray.count > MIN_WIDTH_TYPE ? _myFrame.size.width / _dataArray.count : MIN_WIDTH_TYPE;
        self.backgroundColor = [UIColor color_main_background_color];
        
        [self creatUI];
    }
    return self;
}

/**
 创建UI
 */
- (void) creatUI {
    
    
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, _myFrame.size.width - MORE_BTN_WIDTH, _myFrame.size.height)];
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor color_content_background_color];
    _scrollView.contentSize = CGSizeMake(_width * _dataArray.count, _myFrame.size.height);
    [self addSubview:_scrollView];
    
    if (self.type == B_SelectTypeDefault) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, _myFrame.size.height - 2.5, MIN_WIDTH_TYPE - 20, 2)];
        [_scrollView addSubview:_lineView];
        _lineView.backgroundColor = [UIColor color_text_yellow];
        _lineView.centerX = _width / 2.;
    } else {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _myFrame.size.height - 10)];
        [_scrollView addSubview:_lineView];
        _lineView.backgroundColor = [UIColor color_alert_background_color];
        _lineView.centerX = _width / 2.;
        _lineView.layer.masksToBounds = YES;
        _lineView.layer.cornerRadius = (_myFrame.size.height - 10) / 2.;
        
    }
    
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_dataArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = xgSystemFont(14);
        button.frame = CGRectMake(_width * i, 0, _width, _myFrame.size.height - 10);
        [_scrollView addSubview:button];
        [button setTitleColor:[UIColor color_text_deep_gray] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor color_text_white] forState:UIControlStateSelected];
        if (i == 0) {
            button.selected = YES;
            button.titleLabel.font = xgBoldFont(14);
        }
        button.tag = i + 1;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    

}

- (void)setType:(B_SelectType)type {
    _type = type;
    if (self.type == B_SelectTypeDefault) {
        _lineView.backgroundColor = [UIColor color_text_yellow];
        _lineView.centerX = _width / 2.;
    } else {
        _lineView.frame = CGRectMake(0, 0, _width, _myFrame.size.height - 10);
        _lineView.backgroundColor = [UIColor color_pink];
        _lineView.centerX = _width / 2.;
        _lineView.layer.masksToBounds = YES;
        _lineView.layer.cornerRadius = (_myFrame.size.height - 10) / 2.;
        
    }
}

/**
 按钮的点击事件

 @param btn 按钮
 */
- (void) buttonClicked:(UIButton *) btn {
    _isOpen = YES;
    UIButton * selectBtn;
    /// 查找当前选中的按钮
    for (id subView in _scrollView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            if (((UIButton *)subView).isSelected) {
                selectBtn = subView;
                break;
            }
        }
    }
    if (selectBtn.tag == btn.tag) {
        return;
    }
    [self setSelectBtn:selectBtn willSelectBtn:btn];
    /// 判断当前的位置,是否需要scrollView滚动
//    if ((_scrollView.contentSize.width - _width * (btn.tag - 0.5) >= _scrollView.bounds.size.width / 2.) && (_width * (btn.tag - 0.5) >= _myFrame.size.width / 2.)) {
//
//        btn.selected = YES;
//        selectBtn.selected = NO;
//        btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//        selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//        [UIView animateWithDuration:0.2 animations:^{
//            _scrollView.contentOffset = CGPointMake(_width * (btn.tag - 0.5) - _scrollView.bounds.size.width / 2., 0);
//            _lineView.centerX = _width * (btn.tag - 0.5);
//        }];
//    } else {
//        btn.selected = YES;
//        selectBtn.selected = NO;
//        btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//        selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//        if (_scrollView.contentSize.width - _width * (btn.tag - 0.5) < _myFrame.size.width / 2.) {
//            [UIView animateWithDuration:0.2 animations:^{
//                _scrollView.contentOffset = CGPointMake(_scrollView.contentSize.width - _scrollView.bounds.size.width, 0);
//                _lineView.centerX = _width * (btn.tag - 0.5);
//            }];
//        } else if (_width * (btn.tag - 0.5) < _myFrame.size.width / 2.) {
//            [UIView animateWithDuration:0.2 animations:^{
//                _scrollView.contentOffset = CGPointMake(0, 0);
//                _lineView.centerX = _width * (btn.tag - 0.5);
//            }];
//        }
//    }
//    /// 调用代理方法
//    if (self.delegate && [self.delegate respondsToSelector:@selector(mainSelectTypeWithBtn:)]) {
//        [self.delegate mainSelectTypeWithBtn:btn];
//    }
}

/**
 设置选中的下标

 @param selectIndex 下标
 */
- (void) setSelectIndex:(int)selectIndex {
    _selectIndex = selectIndex;
    UIButton * selectBtn, * willSelectBtn;
    for (id subView in _scrollView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            if (((UIButton *)subView).isSelected) {
                selectBtn = subView;
                break;
            }
        }
    }
    willSelectBtn = (UIButton *) [_scrollView viewWithTag:_selectIndex + 1];
    if (selectBtn.tag == willSelectBtn.tag) {
        return;
    }
    [self setSelectBtn:selectBtn willSelectBtn:willSelectBtn];
    
}


/**
 更多按钮的点击事件

 @param btn 按钮
 */
- (void) moreButtonClicked:(UIButton *) btn {
    HXLog(@"更多按钮点击了");
}


/**
 设置只是标杆的

 @param pointX x的位置,计算之前的
 */
- (void) setFrameOfLineView:(CGFloat) pointX {
    _isOpen = NO;
    _lineView.centerX = (pointX / _myFrame.size.width) * _width  + _width / 2.;
}

/**
 设置选中按钮

 @param pointX 还未计算的点
 */
- (void) setSelectBtn:(CGFloat)pointX {
    UIButton * selectBtn, * willSelectBtn;
    /// 查找当前选中的按钮
    for (id subView in _scrollView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            if (((UIButton *)subView).isSelected) {
                selectBtn = subView;
                break;
            }
        }
    }
    if ((selectBtn.tag - 1) * _myFrame.size.width > pointX) {
        willSelectBtn = (UIButton *)[_scrollView viewWithTag:pointX / _myFrame.size.width + 1];
    } else if ((selectBtn.tag - 1) * _myFrame.size.width < pointX) {
        willSelectBtn = (UIButton *)[_scrollView viewWithTag:pointX / _myFrame.size.width + 1];
    } else {
        return;
    }
    [self setSelectBtn:selectBtn willSelectBtn:willSelectBtn];
}

/**
 设置选中的按钮

 @param selectBtn 已经选中的按钮
 @param btn 将要选中的按钮
 */
- (void) setSelectBtn:(UIButton *) selectBtn willSelectBtn:(UIButton *) btn {
    /// 判断当前的位置,是否需要scrollView滚动
    if ((_scrollView.contentSize.width - _width * (btn.tag - 0.5) >= _scrollView.bounds.size.width / 2.) && (_width * (btn.tag - 0.5) >= _myFrame.size.width / 2.)) {
        
        btn.selected = YES;
        selectBtn.selected = NO;
        btn.titleLabel.font = xgBoldFont(14);
        selectBtn.titleLabel.font = xgSystemFont(14);
        [UIView animateWithDuration:0.2 animations:^{
            _scrollView.contentOffset = CGPointMake(_width * (btn.tag - 0.5) - _scrollView.bounds.size.width / 2., 0);
            if (_isOpen) {
                _lineView.centerX = _width * (btn.tag - 0.5);
            }
        }];
    } else {
        btn.selected = YES;
        selectBtn.selected = NO;
        btn.titleLabel.font = xgBoldFont(14);
        selectBtn.titleLabel.font = xgSystemFont(14);
        if (_scrollView.contentSize.width - _width * (btn.tag - 0.5) < _myFrame.size.width / 2.) {
            [UIView animateWithDuration:0.2 animations:^{
                _scrollView.contentOffset = CGPointMake(_scrollView.contentSize.width - _scrollView.bounds.size.width, 0);
                if (_isOpen) {
                    _lineView.centerX = _width * (btn.tag - 0.5);
                }
            }];
        } else if (_width * (btn.tag - 0.5) < _myFrame.size.width / 2.) {
            [UIView animateWithDuration:0.2 animations:^{
                _scrollView.contentOffset = CGPointMake(0, 0);
                if (_isOpen) {
                    _lineView.centerX = _width * (btn.tag - 0.5);
                }
            }];
        }
    }
    if (_isOpen) {
        /// 调用代理方法
        if (self.delegate && [self.delegate respondsToSelector:@selector(mainSelectTypeWithBtn:)]) {
            [self.delegate mainSelectTypeWithBtn:btn];
        }
    }
}

@end
